import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/com',
    component: () => import('../views/Component')
  },
  { path: '/watch', component: () => import('../views/Watch') },
  {
    path: '/admin',
    component: () => import('../views/Admin'),
    redirect: '/admin/movie',
    children: [
      {
        /* 子级路由中的path不能以斜线(/)开头 */
        path: 'user',
        component: () => import('../views/User')
      },
      {
        path: 'actor',
        component: () => import('../views/Actor')
      },
      {
        path: 'movie',
        component: () => import('../views/Movie')
      }
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

/* 路由守卫 */
router.beforeEach((to, from, next) => {
  /* 该方法中如果不调用next()函数，则路由失效 */
  console.log('---beforeEach---')
  console.log(to.path)
  console.log(from.path)
  console.log('---beforeEach---')
  next()
})

export default router
